package com.k12.admim.controller.operator;

import cn.dev33.satoken.stp.StpUtil;
import com.k12.admim.model.entity.sys.SysPost;
import com.k12.admim.model.vo.sys.SysPostVo;
import com.k12.admim.service.OperatorPostService;
import com.k12.admim.utils.RequestContextUtil;
import com.k12.common.model.resp.R;
import com.k12.common.util.PageUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OperatorPostController {

    @Resource
    private OperatorPostService operatorPostService;


    @GetMapping("system/post/list")
    public R listPost(SysPostVo post){
        String tenantId = RequestContextUtil.getTenantId();
        if(null == tenantId){
            return R.failed("非法登录");
        }
        List<SysPost> list = operatorPostService.listPost(post);
        PageUtil.PageResult<SysPost> paginate = PageUtil.paginate(list, post.getPageNum(), post.getPageSize());
        return R.success(paginate);
    }


    @PostMapping("system/post")
    public R addPost(@Validated @RequestBody SysPostVo post){
        String tenantId = RequestContextUtil.getTenantId();
        if(null == tenantId){
            return R.failed("非法登录");
        }
        if (!operatorPostService.checkPostNameUnique(post))
        {
            return R.failed("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!operatorPostService.checkPostCodeUnique(post))
        {
            return R.failed("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        String loginId1 = String.valueOf(StpUtil.getLoginId());
        post.setCreateBy(loginId1);
        return R.success(operatorPostService.insertPost(post));
    }

    @PutMapping("system/post")
    public R updatePost(@Validated @RequestBody SysPostVo post){
        String tenantId = RequestContextUtil.getTenantId();
        if(null == tenantId){
            return R.failed("非法登录");
        }
        if (!operatorPostService.checkPostNameUnique(post))
        {
            return R.failed("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!operatorPostService.checkPostCodeUnique(post))
        {
            return R.failed("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        String loginId1 = String.valueOf(StpUtil.getLoginId());
        post.setUpdateBy(loginId1);
        return R.success(operatorPostService.updatePost(post));
    }


    /**
     * 根据岗位编号获取详细信息
     */
    @GetMapping(value = "system/post/{postId}")
    public R getInfo(@PathVariable Long postId) {
        String tenantId = RequestContextUtil.getTenantId();
        if(null == tenantId){
            return R.failed("非法登录");
        }
        return R.success(operatorPostService.selectPostById(postId,tenantId));
    }


    /**
     * 删除岗位
     */
    @DeleteMapping("system/post/{postIds}")
    public R remove(@PathVariable Long[] postIds) {
        String tenantId = RequestContextUtil.getTenantId();
        if(null == tenantId){
            return R.failed("非法登录");
        }
        return operatorPostService.deletePostByIds(postIds,tenantId);
    }



}
